package com.jay.compass.app;

import android.annotation.TargetApi;
import android.app.Application;
import android.os.Build;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jay.compass.R;
import com.jay.compass.ble.BluetoothLeService;
import com.jay.compass.bt.BluetoothService;
import com.jay.compass.bt.IBluetoothService;
import com.jay.compass.database.MessageDB;
import com.jay.compass.database.RecentDB;
import com.jay.compass.database.UserDB;
import com.jay.compass.utils.SharePreferenceUtil;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class StarApplication extends Application {
	public static final String SP_FILE_NAME = "push_msg_sp";
	// ======头像===
	public static final int NUM_PAGE = 6;// 总共有多少页
	public static int NUM = 20;// 每页20个表情,还有最后一个删除button
	private static StarApplication mApplication;
	private SharePreferenceUtil mSpUtil;
	private UserDB mUserDB;
	private MessageDB mMsgDB;
	private RecentDB mRecentDB;
	// private List<User> mUserList;
	private static final int[] heads = { R.drawable.head0, R.drawable.head1,
			R.drawable.head2, R.drawable.head3, R.drawable.head4,
			R.drawable.head5, R.drawable.head6, R.drawable.head7,
			R.drawable.head8, R.drawable.head9 };
	private Gson mGson;

	private IBluetoothService bluetoothService;
	private BluetoothLeService bleService;

	public synchronized static StarApplication getInstance() {
		return mApplication;
	}

	@Override
	public void onCreate() {
		System.out.println("application onCreate");
		super.onCreate();
		mApplication = this;
		initData();
	}

	private void initData() {
		mGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		mSpUtil = new SharePreferenceUtil(this, SP_FILE_NAME);
		mUserDB = new UserDB(this);
		mMsgDB = new MessageDB(this);
		mRecentDB = new RecentDB(this);
	}

	public synchronized UserDB getUserDB() {
		if (mUserDB == null)
			mUserDB = new UserDB(this);
		return mUserDB;
	}

	public synchronized Gson getGson() {
		if (mGson == null)
			// 不转换没有 @Expose 注解的字段
			mGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
					.create();
		return mGson;
	}

	public synchronized MessageDB getMessageDB() {
		if (mMsgDB == null)
			mMsgDB = new MessageDB(this);
		return mMsgDB;
	}

	public synchronized RecentDB getRecentDB() {
		if (mRecentDB == null)
			mRecentDB = new RecentDB(this);
		return mRecentDB;
	}

	public synchronized SharePreferenceUtil getSpUtil() {
		if (mSpUtil == null)
			mSpUtil = new SharePreferenceUtil(this, SP_FILE_NAME);
		return mSpUtil;
	}

	public void saveBluetoothService(IBluetoothService bs) {
		bluetoothService = bs;
	}

	public IBluetoothService getBluetoothService() {
		return bluetoothService;
	}

	public void saveBleService(BluetoothLeService bs) {
		bleService = bs;
	}

	public BluetoothLeService getBleService() {
		return bleService;
	}

	public static int[] getHeads() {
		return heads;
	}
}
